#coding: utf-8
from const import TILE_EDGE
import object
#import events

class mController:
    def __init__(self, main, name, file=None, defaultTile='0', boundaries=(100,100)):
        "Первоначальное создание и|или загрузка карты"
        self.map = {}
        self.main = main
        self.name = name
        self.default = defaultTile
        self.boundaries = boundaries
        if file:
            self.file=file
            self._loadmap()

    def passable(self, pos):
        if (pos[0] > self.boundaries[0] or pos[1] > self.boundaries[1] or pos[0] < 0 or pos[1] < 0):
            return False
        else:
            return True

    def invokeEvent(self, actor=None, coord=None, event=None):
        "Вызов события (пока не внедрен)"
        if not event:
            pass
        else:
            if actor and not coord:
                #event(actor, event)
                pass
            if actor and coord: 
                for i in map[coord]:
                    #event(i, event)
                    pass

#     def setTile(self, coords=(0,0), code=0):
#         "Установка определенного тайла (К УДАЛЕНИЮ)"
#         self.map[coords] = code

    def _loadmap(self):
        "Загрузка карты из файла, связанного с объектом"
        pass

    def getMyPos(self, actor):
        "К УДАЛЕНИЮ: Вернуть позицию актера. Скоро не понадобится"
        return (self.main.actors[actor].xpos * TILE_EDGE, self.main.actors[actor].ypos * TILE_EDGE)

    def addActor(self, actor):
        "Добавить актера на карту"
        print "ADDING"
        if self.main.actors[actor].position() in self.map.keys():
            self.map[self.main.actors[actor].position()].append(actor)
        else:
            self.map[self.main.actors[actor].position()] = [actor]

    def getArea(self, id):
        pos = self.main.actors[id].position()
        area = []
        for i in xrange(-5, 6):
            for j in xrange(-5, 6):
                area.append(self.getTile((pos[0]+i, pos[1]+j),(i,j)))
        return area
                
    def getTile(self, pos, shift):
        land = object.Landscape(None, self.default, pos, self.name)
        coord = (TILE_EDGE * 5 + TILE_EDGE * shift[0], TILE_EDGE * 5 + TILE_EDGE * shift[1])
        if (pos[0] > self.boundaries[0] or pos[1] > self.boundaries[1] or pos[0] < 0 or pos[1] < 0):
            return [('empty', coord)]
        ret = [(land.tile(), coord)]
        if pos in self.map.keys():
            for m in self.map[pos]:
                ret.append((self.main.actors[m].tile(), coord))
        return ret
